﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace DoAn_BumTruong_DesignPattern
{
    public class DBContextEntity:DbContext
    {
        //Tao bang du lieu trong CSDL
        public DbSet<Article> Articles { get; set; }
        public DbSet<Menu> Menues { get; set; }
        public DbSet<Comments> Commentes { get; set; }
        public DbSet<Module> Modules { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //CO nghia khi du lieu cua bang co thay doi thi no se thay doi duoc. OK
            Database.SetInitializer<DBContextEntity>(new DropCreateDatabaseIfModelChanges<DBContextEntity>());
            //Co the kiem tra cac truong thuoc tinh trong cac bang nhu bo dau tich bat buoc phai nhap du lieu trong CSDL...
            //Article
            modelBuilder.Entity<Article>().Property(ar => ar.Title).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            modelBuilder.Entity<Article>().Property(ar => ar.Alias).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            modelBuilder.Entity<Article>().Property(ar => ar.IntroText).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            modelBuilder.Entity<Article>().Property(ar => ar.FullText).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            modelBuilder.Entity<Article>().Property(ar => ar.Title).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            modelBuilder.Entity<Article>().Property(ar => ar.Title).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            //Menu
            modelBuilder.Entity<Menu>().Property(mn => mn.Name).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            modelBuilder.Entity<Menu>().Property(mn => mn.Alias).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            //Module
            modelBuilder.Entity<Module>().Property(md => md.Name).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            modelBuilder.Entity<Module>().Property(md => md.Path).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
            modelBuilder.Entity<Module>().Property(md => md.PositionDisplay).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
           
            //Comment
            modelBuilder.Entity<Comments>().Property(cm=>cm.Context).IsRequired();//Bo dau tich truong Title trong CSDL. Tuong tu voi nhung thuoc tinh nao thich bo
        
        }
    }
}